Skip to content

add current directory to sys.path for Windows module resolution#299

Closed
MohammadRaziei wants to merge 1 commit intopybind:mainfrom
MohammadRaziei:main
Closed

add current directory to sys.path for Windows module resolution#299
MohammadRaziei wants to merge 1 commit intopybind:mainfrom
MohammadRaziei:main

Conversation

@MohammadRaziei
Copy link
Copy Markdown

On Windows, when running pybind11-stubgen from a build directory
(e.g., via CMake), the compiled module (.pyd file) cannot be found
because it's not in Python's search path.

This fix adds the current working directory to sys.path before
importing the module, ensuring compatibility with Windows builds
where the module exists in the build directory.

Example on Windows:
build/src/mymodule.cp311-win_amd64.pyd ← exists but not found

Without this fix:
ModuleNotFoundError: No module named 'mymodule'

With this fix:
Module imports successfully ✓

Insert the current working directory at the beginning of sys.path before processing modules. This ensures that local modules can be resolved correctly when pybind11-stubgen is run, preventing import errors for modules located in the current directory.
@sizmailov
Copy link
Copy Markdown
Collaborator

#77 (comment)

@MohammadRaziei
Copy link
Copy Markdown
Author

Why do you think this is not a good idea?? I believe that this is a necessary option, and I was shocked when I found that this was missed.

@ax3l ax3l added invalid This doesn't seem right wontfix This will not be worked on labels Apr 20, 2026
@ax3l
Copy link
Copy Markdown
Contributor

ax3l commented Apr 20, 2026

Sorry, such inserts have a lot of unwanted side effects when used in larger or integrated projects.

We will close this, because this would introduce an anti-pattern:
Python’s import path is defined at interpreter startup, and PYTHONPATH is the standard way to add import locations. Console-script execution does not automatically treat the working directory as sys.path[0] the same way python -m ... does. That behavior is documented here: https://docs.python.org/3/library/sys_path_init.html

If any code change were acceptable, it would need to be explicit and opt-in, not a hidden global sys.path mutation. But in this case, no code change is needed; the build or test environment should make the extension importable before pybind11-stubgen runs.

For example, in CMake:

add_custom_target(stubs
    COMMAND ${CMAKE_COMMAND} -E env
            "PYTHONPATH=$<TARGET_FILE_DIR:mymodule>"
            ${Python_EXECUTABLE} -m pybind11_stubgen mymodule
            -o ${CMAKE_CURRENT_BINARY_DIR}/stubs
    DEPENDS mymodule
)

The same pattern works for add_test(...) as well (example).

@ax3l ax3l closed this Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

invalid This doesn't seem right wontfix This will not be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants